package com.dong.netty.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author dengxiudong
 * @Date 2024/7/24 上午10:28
 */
@Slf4j
class NettyClientHandler extends SimpleChannelInboundHandler<String>
{

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        // 连接建立后，可以向服务器发送JSON消息
        // String json = "{\n" +
        //         "    \"devId\": \"SDZK482004V10202306000541\",\n" +
        //         "    \"devType\": 2,\n" +
        //         "    \"softVersion\": \"1.1.6\",\n" +
        //         "    \"txnNo\": 1721709231064,\n" +
        //         "    \"iccid\": \"\",\n" +
        //         "    \"msgType\": 110,\n" +
        //         "    \"hardVersion\": \"4\",\n" +
        //         "    \"protocolVersion\": \"V1\",\n" +
        //         "    \"imsi\": \"\",\n" +
        //         "    \"switchCabStatus\": 1\n" +
        //         "}";

        String json = "{\n" +
                "  \"devId\": \"SDZK482004V10202306000541\",\n" +
                "  \"devType\": 2,\n" +
                "  \"softVersion\": \"1.1.6\",\n" +
                "  \"txnNo\": 1721709231064,\n" +
                "  \"iccid\": {\n" +
                "    \"aa\": 11,\n" +
                "    \"bb\": 22,\n" +
                "    \"cc\": 33\n" +
                "  },\n" +
                "  \"msgType\": 110,\n" +
                "  \"hardVersion\": \"4\",\n" +
                "  \"protocolVersion\": \"V1\",\n" +
                "  \"imsi\": \"\",\n" +
                "  \"switchCabStatus\": 166666\n" +
                "}{\n" +
                "  \"devId\": \"SDZK482004V10202306000541\",\n" +
                "  \"devType\": 2,\n" +
                "  \"softVersion\": \"1.1.6\",\n" +
                "  \"txnNo\": 1721709231064,\n" +
                "  \"iccid\": {\n" +
                "    \"aa\": 11,\n" +
                "    \"bb\": 22,\n" +
                "    \"cc\": 33\n" +
                "  },\n" +
                "  \"msgType\": 110,\n" +
                "  \"hardVersion\": \"4\",\n" +
                "  \"protocolVersion\": \"V1\",\n" +
                "  \"imsi\": \"\",\n" +
                "  \"switchCabStatus\": 188888\n" +
                "}{\n" +
                "  \"devId\": \"SDZK482004V10202306000541\",\n" +
                "  \"devType\": 2,\n" +
                "  \"softVersion\": \"1.1.6\",\n" +
                "  \"txnNo\": 1721709231064,\n" +
                "  \"iccid\": {\n" +
                "    \"aa\": 11,\n" +
                "    \"bb\": 22,\n" +
                "    \"cc\": 33\n" +
                "  },\n" +
                "  \"msgType\": 110,\n" +
                "  \"hardVersion\": \"4\",\n" +
                "  \"protocolVersion\": \"V1\",\n" +
                "  \"imsi\": \"\",\n" +
                "  \"switchCabStatus\": 19999\n" +
                "}";
        ctx.writeAndFlush(json);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, String s) throws Exception
    {
        // 处理从服务器接收到的消息
        log.info("Received message from server: {}", s);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        log.error("Exception in NettyClientHandler: ", cause);
        ctx.close();
    }
}
